# coding=utf-8
__author__ = 'liubin'

from flask import render_template
from flask.views import View
from flask.ext.login import login_required
from flask.ext.restful import Resource, marshal
from project import app, Role, api_view
from resource import role_resource_fields

class RoleListView(View):
    """
    权限列表的模板
    """
    methods = ['GET']
    def dispatch_request(self):
        return render_template('role/list.html')

# url指定
app.add_url_rule('/tpl/role', view_func=RoleListView.as_view('tpl_rolelist'))

class RoleListApi(Resource):
    method_decorators = [login_required]

    def get(self, **kwargs):
        data = []
        for role in Role.query.all():
            data_role = {
                'id': role.id,
                'code': role.code,
                'name': role.name
            }
            data.append(marshal(data_role, role_resource_fields))

        return {'success': True,  'data': data}
api_view.add_resource(RoleListApi, '/api/role', endpoint="api_rolelist")